/**
 * 北京钉图互动科技 all right reserver
 */
package com.impress.commons.vo;

import java.io.Serializable;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnore;

import io.swagger.annotations.ApiModelProperty;

/**
 * @author Wangyi
 * @since 2016年6月13日
 */
abstract class Chunk<T> implements Iterable<T>, Serializable {

	private static final long serialVersionUID = 5838558790077086620L;

	@ApiModelProperty(value = "数据内容")
	private List<T> content;

	public Chunk() {
	}

	public Chunk(List<T> content) {
		this.content = content;
	}

	@Override
	public Iterator<T> iterator() {
		if (content == null) {
			Collections.emptyIterator();
		}
		return content.iterator();
	}

	/**
	 * @return the content
	 */
	public List<T> getContent() {
		return content;
	}

	/**
	 * @param content the content to set
	 */
	public void setContent(List<T> content) {
		this.content = content;
	}

	@JsonIgnore
	public int getContentSize() {
		return content != null ? content.size() : 0;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {

		if (this == obj) {
			return true;
		}

		if (!(obj instanceof Chunk<?>)) {
			return false;
		}

		Chunk<?> that = (Chunk<?>) obj;

		return this.content.equals(that.content);
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {

		int result = 17;

		result += 31 * content.hashCode();

		return result;
	}
}
